import UIKit

//
//
////Reflect
//class User:NSObject{
//  @objc var name:String = "" //姓名
//    var nickName:String? //昵称
//    var age:Int? //年龄
//    var emails:[String]? //邮件地址
//}
//let user1 = User()
//    user1.name = "老王"
//    user1.age = 10000
//    user1.emails = ["laowang@qq.com","laownag@163.com"]
//let name = user1.value(forKey: "name")
//
//let hMirror = Mirror(reflecting: user1)
//print("对象类型:\(hMirror.subjectType)")
//print("对象元素个数\(hMirror.children.count)")
//print("-------对象元素属性和属性值分别如下----------")
//for case let (label?,value) in hMirror.children {
//    print("属性:\(label) 值:\(value)")
//}
//
//
////Reflect
//class User:NSObject{
//  @objc dynamic var name:String = "" //姓名
//  @objc dynamic var nickName:String? //昵称
//    var age:Int? //年龄
//    var emails:[String]? //邮件地址
//}
//let user1 = User()
//    user1.name = "老王"
//    user1.age = 10000
//    user1.emails = ["laowang@qq.com","laownag@163.com"]
//let keyPath = \User.name
//let name = user1[keyPath:keyPath]
//print(name)
//user1[keyPath:keyPath] = "laowang.com"
////KeyPath
//
//let hMirror = Mirror(reflecting: user1)
//print("对象类型:\(hMirror.subjectType)")
//print("对象元素个数\(hMirror.children.count)")
//print("-------对象元素属性和属性值分别如下----------")
//for case let (label?,value) in hMirror.children {
//    print("属性:\(label) 值:\(value)")
//}
//
//// 通过属性名获取对应的属性值，并对值做出类型o判断(包括是否为空)
//func getValueByKey(obj:AnyObject,key:String) -> Any {
//    let hMirror = Mirror(reflecting: obj)
//    for case let (label?,value) in hMirror.children {
//        if label == key {
//            return unwrap(value)
//        }
//    }
//    return NSNull()
//}
//func unwrap(_ any:Any) -> Any {
//    let mi = Mirror(reflecting: any)
//    if mi.displayStyle != .optional {
//        return any
//    }
//    if mi.children.count == 0 {return any}
//    let (_,some) = mi.children.first!
//    return some
//}
//
//struct GenericDictionary<Key:Hashable,Value> {
//    private var data:[Key:Value]
//
////序列化代码
//struct Lanuager:Codable {
//    var name:String
//    var version:Int
//}
//let swift = Lanuager(name: "Swift", version: 4)
////encode 对象
//let encodeData = try JSONEncoder().encode(swift)
//
////从encode对象中获取string
//let jsonString = String(data: encodeData, encoding:.utf8)
//print(jsonString)
////Decode 操作
//let decodeData = try JSONDecoder().decode(Lanuager.self, from: encodeData)
//print(decodeData.name,decodeData.version)
////下标运算
//struct GenericDictionary<Key:Hashable,Value> {
//    private var data:[Key:Value]
//
//
//    init(data:[Key:Value]) {
//        self.data = data
//    }
//    subscript<T>(key:Key) -> T? {
//        return data[key] as? T
//    }
//}
//
////字典类型
//let eartData = GenericDictionary(data: ["name":"Earth","population":750000000000,"moons":1])
//let name:String? = eartData["name"]
//print(name)
//let population:Int? = eartData["population"]
//print(population)
//=======
////字典类型[String:Any]
//let earthData  = GenericDictionary(data: ["name":"Earth","population":7500000000,"moons":1])
//
////自动类型转换,不需要在写 “as? String
//let nameA:String? = earthData["name"]
//
//
//
//
//
//

//二叉树
indirect enum BinarySearchTree<Element:Comparable> {
    case leaf
    case node(BinarySearchTree<Element>,Element,BinarySearchTree<Element>)
}
let leaf:BinarySearchTree<Int> = .leaf
let five:BinarySearchTree<Int> = .node(leaf,5,leaf)
extension BinarySearchTree {
    init() {
        self = .leaf
    }
    init(value:Element) {
        self = .node(.leaf,value,.leaf)
    }
}
extension BinarySearchTree {
    var count:Int {
        switch self {
        case .leaf:
            return 0
        case let .node(left,_,right):
            return 1 + left.count + right.count
        }
    }
}

var  name = ["Jemima","Peter","David","Kelly","Isabella"]
     name.sort()

